package com.yiren.backstage.communication.services.impl;

import android.content.Context;

import com.yiren.backstage.communication.services.IAnalysisStateService;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.enums.CarStateNameEnum;
import com.yiren.backstage.currency.enums.CarStateValueEnum;

/**
 * Time:2021/11/24 8:54
 * Author:xiaozan
 * Description:解析车辆状态值 业务实现类
 */
public class AnalysisStateServiceImpl implements IAnalysisStateService {

    private Context context;
    public static int countLeftLightRepeat = 0;     //计算左转灯数据重复次数
    public static int countRightLightRepeat = 0;    //计算右转灯数据重复次数
    public static int countCautionLightRepeat = 0;  //计算警示灯数据重复次数
    public static long countRainWipperRepeat = 0;   //计算雨刮器数据重复时间

    private CarStateValueEnum currentStateValue; //当前车辆状态值

    public AnalysisStateServiceImpl(Context context) {
        this.context = context;
    }

    @Override
    public CarStateValueEnum openAndCloseState(CarStateNameEnum carStateNameEnum, boolean carStateSignalValue, int value) {
        if (value == 1) {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.OPEN : CarStateValueEnum.CLOSE;
        } else {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.CLOSE : CarStateValueEnum.OPEN;
        }
        return currentStateValue;
    }

    @Override
    public CarStateValueEnum loosenAndCockingState(CarStateNameEnum carStateNameEnum, boolean carStateSignalValue, int value) {
        if (value == 1) {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.COCKING : CarStateValueEnum.LOOSEN;
        } else {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.LOOSEN : CarStateValueEnum.COCKING;
        }
        return currentStateValue;
    }

    @Override
    public CarStateValueEnum brakeingAndLoosenState(CarStateNameEnum carStateNameEnum, boolean carStateSignalValue, int value) {
        if (value == 1) {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.STEP_ON : CarStateValueEnum.LOOSEN;
        } else {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.LOOSEN : CarStateValueEnum.STEP_ON;
        }
        return currentStateValue;
    }

    @Override
    public CarStateValueEnum hitchAndLoosenState(CarStateNameEnum carStateNameEnum, boolean carStateSignalValue, int value) {
        if (value == 1) {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.HITCH : CarStateValueEnum.LOOSEN;
        } else {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.LOOSEN : CarStateValueEnum.HITCH;
        }
        return currentStateValue;
    }

    @Override
    public CarStateValueEnum adjustAndNoAdjustState(CarStateNameEnum carStateNameEnum, boolean carStateSignalValue, int value) {
        if (value == 1) {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.ADJUST : CarStateValueEnum.NOT_ADJUST;
        } else {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.NOT_ADJUST : CarStateValueEnum.ADJUST;
        }
        return currentStateValue;
    }

    @Override
    public CarStateValueEnum passAndNoPassState(CarStateNameEnum carStateNameEnum, boolean carStateSignalValue, int value) {
        if (value == 1) {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.PASS : CarStateValueEnum.NO_PASS;
        } else {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.NO_PASS : CarStateValueEnum.PASS;
        }
        return currentStateValue;
    }

    @Override
    public CarStateValueEnum touchAndNoTouchState(CarStateNameEnum carStateNameEnum, boolean carStateSignalValue, int value) {
        if (value == 1) {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.TOUCH : CarStateValueEnum.NOT_TOUCH;
        } else {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.NOT_TOUCH : CarStateValueEnum.TOUCH;
        }
        return currentStateValue;
    }

    @Override
    public CarStateValueEnum carKeyState(CarStateNameEnum carStateNameEnum, boolean carStateSignalValue, int value) {
        if (value == 1) {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.KEY_START : CarStateValueEnum.KEY_LOCK_OFF;
        } else {
            currentStateValue = carStateSignalValue ? CarStateValueEnum.KEY_LOCK_OFF : CarStateValueEnum.KEY_START;
        }
        return currentStateValue;
    }

    @Override
    public float jxSpeed(String odbValue, float carStateSignalValue, String jxValue) {
        float speed = 0;
        if (Float.parseFloat(odbValue) > 0) {
            speed = Float.parseFloat(odbValue);
        } else {
            speed = Float.parseFloat(jxValue);
            speed = (float) ((speed * 72 * carStateSignalValue) / 1000);
        }
        return speed;
    }

    @Override
    public float jxRotateSpeed(String odbValue, float carStateSignalValue, String jxValue) {
        //接线版转速分为两种，第一种：通过OBD获取 第二种：通过接线方式获取
        float rotateSpeed = 0;
        if (Float.parseFloat(odbValue) > 0) {
            rotateSpeed = Float.parseFloat(odbValue);
        } else {
            rotateSpeed = Float.parseFloat(jxValue);
            rotateSpeed = (float) (rotateSpeed * carStateSignalValue);
        }
        return rotateSpeed;
    }

    @Override
    public CarStateValueEnum carKeyState(byte[] value) {
        CarStateValueEnum currentState = CarStateValueEnum.KEY_ON;
        // 钥匙 :00表示LOCK/OFF，01表示ACC,10表示START,11表示ON
        if (value[6] == 0 && value[7] == 0) {
            currentState = CarStateValueEnum.KEY_LOCK_OFF;
        }
        if (value[6] == 0 && value[7] == 1) {
            currentState = CarStateValueEnum.KEY_ACC;
        }
        if (value[6] == 1 && value[7] == 0) {
            currentState = CarStateValueEnum.KEY_START;
        }
        if (value[6] == 1 && value[7] == 1) {
            currentState = CarStateValueEnum.KEY_ON;
        }
        return currentState;
    }

    @Override
    public CarStateValueEnum obdGear(int value) {
        CarStateValueEnum currentGear = CarStateValueEnum.NEUTRAL_GEAR;
        if (value == 0) {
            //手动挡空挡
            currentGear = CarStateValueEnum.NEUTRAL_GEAR;
        } else if (value == 1) {
            //手动挡1挡
            currentGear = CarStateValueEnum.ONE_GEAR;
        } else if (value == 2) {
            //手动挡2挡
            currentGear = CarStateValueEnum.TWO_GEAR;
        } else if (value == 3) {
            //手动挡3挡
            currentGear = CarStateValueEnum.THREE_GEAR;
        } else if (value == 4) {
            //手动挡4挡
            currentGear = CarStateValueEnum.FOUR_GEAR;
        } else if (value == 5) {
            //手动挡5挡
            currentGear = CarStateValueEnum.FIVE_GEAR;
        } else if (value == 6) {
            //手动挡倒挡
            currentGear = CarStateValueEnum.REVERSE_GEAR;
        } else if (value == 7) {
            //自动挡P挡
            currentGear = CarStateValueEnum.ONE_GEAR;
        } else if (value == 8) {
            //自动挡R挡
            currentGear = CarStateValueEnum.REVERSE_GEAR;
        } else if (value == 9) {
            //自动挡N
            currentGear = CarStateValueEnum.NEUTRAL_GEAR;
        } else if (value == 10) {
            //自动挡D挡
            currentGear = CarStateValueEnum.TWO_GEAR;
        }
        /*if (isMpGear) {
            currentGear = carState.getGear();
        }
        */
        return currentGear;
    }

    @Override
    public CarStateValueEnum corneringLamp(CarStateNameEnum carStateNameEnum, boolean carStateSignalValue, int value) {
        CarStateValueEnum carStateValueEnum = openAndCloseState(carStateNameEnum, carStateSignalValue, value);
        switch (carStateNameEnum) {
            case LEFT_LIGHT:
                if (carStateValueEnum == CarStateValueEnum.OPEN) {
                    countLeftLightRepeat = 0;
                    return carStateValueEnum;
                } else {
                    countLeftLightRepeat++;
                    if (countLeftLightRepeat > SignalGConstant.lightCoefficient) {
                        countRightLightRepeat = 0;
                        return carStateValueEnum;
                    }
                }
                break;
            case RIGHT_LIGHT:
                if (carStateValueEnum == CarStateValueEnum.OPEN) {
                    countRightLightRepeat = 0;
                    return carStateValueEnum;
                } else {
                    countRightLightRepeat++;
                    if (countRightLightRepeat > SignalGConstant.lightCoefficient) {
                        countRightLightRepeat = 0;
                        return carStateValueEnum;
                    }
                }
                break;
//            case CAUTION_LIGHT:
//                if (carStateValueEnum == CarStateValueEnum.OPEN) {
//                    countCautionLightRepeat = 0;
//                    return carStateValueEnum;
//                } else {
//                    countCautionLightRepeat++;
//                    if (countCautionLightRepeat > SignalGConstant.lightCoefficient) {
//                        countCautionLightRepeat = 0;
//                        return carStateValueEnum;
//                    }
//                }
//                break;
            case RAIN_WIPPER:
                if (carStateValueEnum == CarStateValueEnum.OPEN) {
                    countRainWipperRepeat = System.currentTimeMillis();
                    return carStateValueEnum;
                } else {
                    if (System.currentTimeMillis() - countRainWipperRepeat > SignalGConstant.rainWipperCoefficient) {
                        countRainWipperRepeat = System.currentTimeMillis();
                        return carStateValueEnum;
                    }
                }
                break;

        }
        return CarStateValueEnum.CLOSE;
    }
}
